import ika
import direction
import map
from entity import IkaEntity
    
DEFAULT_HURT_DELAY = 50

class MapEntity(IkaEntity):
    dead = False
    health = max_health = 1
    hurt_timer = 0
    hurt_delay = DEFAULT_HURT_DELAY
    
    def Update(self):
        if self.hurt_timer:
            self.visible = self.hurt_timer / 8 % 2 == 0
            self.hurt_timer -= 1
            if not self.hurt_timer:
                if self.dead:
                    self.visible = False
                else:
                    self.visible = True
        super(MapEntity, self).Update()
    
    def Heal(self, amount):
        # Nothing being restored, or negative amount.
        if amount <= 0:
            return
            
        self.health += amount
        if self.health > self.max_health:
            self.health = self.max_health
    
    def Hurt(self, damage):
        # Negative damage isn't allowed.
        # But, zero damage is a stun attack, so we permit that.
        if damage < 0:
            return
        if self.dead or self.hurt_timer > 0:
            return
            
        print "Hurt " + str(self) + " for " + str(damage) + " damage"
            
        self.health -= damage
        self.hurt_timer = self.hurt_delay
        print str(self.hurt_delay) + " delay"
        if self.health <= 0:
            self.Die()
        
    def Die(self):
        # Already dead!
        if self.dead:
            return
        self.health = 0
        self.dead = True
        self.visible = False
        self.is_obstruction = False
        print "dead"
